package com.erp.erp_ui.CustomerContact;

import com.erp.erp_entitys.CustomerContact.CustomerContactEntity;
import com.erp.erp_entitys.CustomerContact.CustomerContactExample;
import com.erp.erp_entitys.CustomerContact.CustomerContactVoEntity;
import com.erp.erp_entitys.CustomerLog.CustomerLogEntity;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_servers.Customer.ICustomerService;
import com.erp.erp_servers.CustomerContact.ICustomerContactService;
import com.erp.erp_servers.CustomerLog.ICustomerLogService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.DateUtils;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 客户联系人
 * @ClassName CustomerContactControl
 * @Description TODO
 * @Author songjizhen
 * @Date 2020-07-03 23:07
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "customercontact")
public class CustomerContactControl extends Base {
    @Autowired
    ICustomerContactService customerContactService;
    @Autowired
    ICustomerService customerService;
    @Autowired
    ICustomerLogService customerLogService;

    /**
     * 分页查询
     * @param
     * @return
     */
    @RequestMapping(value = "selectByPage")
    @ResponseBody
    public R<List<CustomerContactVoEntity>> selectByPage(String company){
        try{
            //UserEntity user = (UserEntity)this.getBean("user");
           // String name=user.getName();
           // List<CustomerContactVoEntity> list=new ArrayList<CustomerContactVoEntity>();
           // CustomerContactExample example=new CustomerContactExample();
            //CustomerContactExample.Criteria criteria = example.createCriteria();
            List<CustomerContactVoEntity> customerContactEntities=new ArrayList<CustomerContactVoEntity>();
            if(!company.equals("")){
               // criteria.andCompanyEqualTo(company);
                 customerContactEntities = customerContactService.query(company);//customerContactService.selectByExample(example);
                /*for(int i=0;i<customerContactEntities.size();i++){
                    CustomerContactVoEntity vo=new CustomerContactVoEntity();
                    vo.convet(customerContactEntities.get(i));
                    list.add(vo);
                }*/
            }
            return R.build(customerContactEntities);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据客户编码，分页查询联系人
     * @param company
     * @return
     */
    @RequestMapping(value = "queryByPage")
    @ResponseBody
    public R<List<CustomerContactEntity>> queryByPage(String company){
        try {
            CustomerContactExample example=new CustomerContactExample();
            CustomerContactExample.Criteria criteria = example.createCriteria();
            String[] companys=company.split(",");
            List<String> list=new ArrayList<>();
            for(String cs:companys){
                list.add(cs);
            }
            criteria.andCompanyIn(list);
            List<CustomerContactEntity> customerContactEntities = customerContactService.selectByExample(example);
            return R.build(customerContactEntities);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }
    /**
     * 分页查询
     * @param
     * @return
     */
    @RequestMapping(value = "select2ByPage")
    @ResponseBody
    public R<List<CustomerContactVoEntity>> select2ByPage(String company,String name,String email){
        try{
            UserEntity user = (UserEntity)this.getBean("user");
            // String name=user.getName();
           /* List<CustomerContactVoEntity> list=new ArrayList<>();
            List<String> list_comp=new ArrayList<String>();
            if(company.equals("")){
               // return R.build(list);
                CustomerExample example=new CustomerExample();
                CustomerExample.Criteria criteria = example.createCriteria();
                criteria.andSalesmanLike("%"+user.getCode()+"%");
                List<CustomerEntity> customerEntities = customerService.selectByExample(example);
                for(int i=0;i<customerEntities.size();i++){
                    list_comp.add(customerEntities.get(i).getCode());
                }
            }else{
                list_comp.add(company);
            }
            if(list_comp.size()==0){
                return R.build(list);
            }*/
           /* CustomerContactExample example=new CustomerContactExample();
            CustomerContactExample.Criteria criteria = example.createCriteria();
            criteria.andNameLike("%"+name+"%");
            criteria.andEmailLike("%"+email+"%");*/
           // criteria.andCompanyIn(list_comp);
            //criteria.andSalesmanLike("%"+user.getCode()+"%");
           // List<CustomerContactEntity> customerContactEntities = customerContactService.selectByExample(example);
           /* for(int i=0;i<customerContactEntities.size();i++){
                CustomerContactVoEntity vo=new CustomerContactVoEntity();
                vo.convet(customerContactEntities.get(i));
                list.add(vo);
            }*/
            List<CustomerContactVoEntity> query = customerContactService.query(name, email, user.getCustomerRole());
            return R.build(query);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
    public R<Integer> delete(String[] ids){
        try {
            List<Integer> list=new ArrayList<>();

            for(int i=0;i<ids.length;i++){
                list.add(Integer.parseInt(ids[i].replace("[","").replace("]","")));
            }
            CustomerContactExample example=new CustomerContactExample();
            CustomerContactExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(list);
            List<CustomerContactEntity> customerContactEntities = customerContactService.selectByExample(example);
            Integer num = customerContactService.deleteByExample(example,list);
//日志
            UserEntity user = (UserEntity)this.getBean("user");

            String codes="";
            for(int i=0;i<customerContactEntities.size();i++){
                codes+=customerContactEntities.get(i).getCompany()+",";
            }
            CustomerLogEntity log=new CustomerLogEntity();
            log.setCode(codes);
            log.setType("删除");
            log.setOptioner(user.getCode());
            log.setOptionTime(DateUtils.format(new Date()));
            log.setContent("联系人所属客户编码："+codes);
            customerLogService.insert(log);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据id获取联系人
     * @param id
     * @return
     */
    @RequestMapping(value = "queryByid",method = RequestMethod.POST)
    @ResponseBody
    public R<List<CustomerContactVoEntity>> queryByid(Integer id){
        try {
            List<CustomerContactVoEntity> list=new ArrayList<>();
            CustomerContactExample example=new CustomerContactExample();
            CustomerContactExample.Criteria criteria = example.createCriteria();
            criteria.andIdEqualTo(id);
            List<CustomerContactEntity> customerContactEntities = customerContactService.selectByExample(example);
            for(int i=0;i<customerContactEntities.size();i++){
                CustomerContactVoEntity vo=new CustomerContactVoEntity();
                vo.convet(customerContactEntities.get(i));
                list.add(vo);
            }
            return R.build(list);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
    public  R<Integer> insert(@RequestBody CustomerContactEntity recode){
        try {
            int i =0;
            if(!(recode.getId()==null)){//id存在就更新
               i = customerContactService.updateByPrimaryKeySelective(recode);
            }else{
                i = customerContactService.insertSelective(recode);
            }
            //日志
            UserEntity user = (UserEntity)this.getBean("user");
            CustomerLogEntity log=new CustomerLogEntity();
            log.setCode(recode.getCompany());
            log.setType("新增");
            log.setOptioner(user.getCode());
            log.setOptionTime(DateUtils.format(new Date()));
            log.setContent("联系人所属客户编码："+recode.getCompany());
            customerLogService.insert(log);
            return R.build(i);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    @RequestMapping(value = "test" )
    @ResponseBody
    public void test(String email){

        //int i = customerContactService.updateByEmail("18829260705@163.com");
       // System.out.println(i);
    }

}
